package jinyilw.swing.visuals.Icomp;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.SystemTools;
import jinyilw.swing.rlaf.UISettable;
import jinyilw.swing.rlaf.UITools;

import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicLabelUI;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import java.net.URISyntaxException;

public class ILinkLabel extends ILabel// 链接标签
{
	private static final long serialVersionUID = -2398761301620451347L;
	private static final ColorSet colorSet = new ColorSet();
	private URI uri;

	public ILinkLabel(String link)
	{
		super(link);
		try
		{
			uri = new URI(link);
		} catch (URISyntaxException e)
		{
			e.printStackTrace();
		}
		setFont(FontTools.defaultFont);
		setCursor(ComponentTools.handCursor);
		addMouseListener(new LinkLabelMouseAdapter());
	}

	private class LinkLabelMouseAdapter extends MouseAdapter
	{
		@Override
		public void mouseClicked(MouseEvent e)
		{
			SystemTools.browseURI(uri);
		}
	}

	@Override
	public void updateUI()
	{
		setUI(new LinkLabelUI());
	}

	static class ColorSet implements UISettable
	{
		public Color textColor;

		private ColorSet()
		{
			UITools.installUITheme(this);
		}

		@Override
		public void lightTheme()
		{
			textColor = Color.BLUE;
		}

		@Override
		public void darkTheme()
		{
			textColor = Color.GREEN;
		}

		@Override
		public void blackTheme()
		{
			textColor = Color.GREEN.darker();
		}
	}

	static class LinkLabelUI extends BasicLabelUI
	{
		@Override
		public void update(Graphics g, JComponent c)
		{
			c.setForeground(colorSet.textColor);
			super.update(g, c);
		}
	}
}
